extends Area2D

# 敌人属性
var hp = 1000  # 生命值
@export var speed = 80  # 移动速度
@export var score_value = 1000  # 击杀得分

# 爆炸效果场景
@export var explosion_scene: PackedScene = preload("res://Effects/Explosion.tscn")

func _ready():
	hp = 10 + 1000 * $"..".EnemyHpB  # 生命值
	add_to_group("enemy")
	

func _physics_process(delta):
	# 敌人向下移动
	position.y += speed * delta

	# 如果敌人移出屏幕，销毁
	if position.y > get_viewport_rect().size.y + 50:
		var main_player = get_tree().get_nodes_in_group("player")[0]
		if main_player:
			main_player.game_over()
		queue_free()

# 处理伤害
func take_damage(dmg):
	hp -= dmg
	print(hp)
	if hp <= 0:
		explode()

# 爆炸效果
func explode():
	if explosion_scene:
		var explosion = explosion_scene.instantiate()
		explosion.position = position
		get_parent().add_child(explosion)
	
	$"..".add_score(score_value)  # 增加分数
	queue_free()  # 销毁敌人


func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("player_bullet"):  # 确保子弹有 "player_bullet" 组
		take_damage($"../PlayerManager".attack_damage)
		area.queue_free()  # 销毁子弹



func _on_body_entered(body: Node2D) -> void:
	if body.is_in_group("player"):
		var main_player = get_tree().get_nodes_in_group("player")[0]
		if main_player:
			main_player.game_over()
